﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Configuration;
using System.IO;
using System.Collections;
using System.Security;
using System.Text.RegularExpressions;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Text = "0";
            int i = 0;
            Console.WriteLine("Please input the string you want to search");
            Text = Console.ReadLine();

            Console.WriteLine("Please choose the language you want to search in:1.Chinese;2.WestLanguage;3.Both:");
            i = int.Parse(Console.ReadLine());
            SpiderTest sp = new SpiderTest();
            sp.getJumpUrl(Text);
            switch (i)
            {
                case 1:
                    sp.CnSpider(Text);
                    break;
                case 2:
                    sp.WeSpider(Text);
                    break;
                case 3:
                    sp.CnSpider(Text);
                    sp.WeSpider(Text);
                    break;
            }


            Console.ReadKey();
        }
    }

    class SpiderTest
    {
        public string jumpUrl = "";
        public void getJumpUrl(string searchText)
        {
            //获取跳转参数
            Uri jurl = new Uri("http://opac.lib.whu.edu.cn/F/?func=find-m&find_code=WRD&request="+searchText+"&pds_handle=GUEST");
            //起始地址
            string jpage = "0";
            string j = @"url=(.*?)\/F\/(.*?)\?";// "url=(http:.*?)';";//@"URL=(.*?)F\/(.*?)\?(.*?)>";//"url=(http:.*?)';";//@"URL=(.*?)F\/(.*?)\?(.*?)>";
            HttpWebRequest requestPage = (HttpWebRequest)WebRequest.Create(jurl);
            WebResponse response = requestPage.GetResponse();
            Stream stream = response.GetResponseStream();//获取页面流
            if (response.ContentType.ToLower().StartsWith("text/"))//如果获得成功（即为文本格式）
            {
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);//GetEncoding("GB2312"));//读取获得内容流new Regex("<div class=itemtitle><a href=(.+?)>(.+?)</a>?")
                jpage = reader.ReadToEnd();
                Regex jumpp = new Regex(j);
                Match jumpPage = jumpp.Match(jpage);//进行匹配
                if (jumpPage.Success)
                {
                    Group jumpg = jumpPage.Groups[2];
                    jumpUrl = jumpg.ToString();
                    Console.WriteLine(jumpUrl);
                }
                else
                    jpage = "匹配错误";
            }
            else
                jpage = "读取页面错误";

        }

        public void CnSpider(string searchText)
        {
            //中文搜索
            Console.WriteLine("The Books in Chinese stack are");
            Uri url = new Uri("http://opac.lib.whu.edu.cn/F/" + jumpUrl + "?func=find-b&find_code=WRD&request="+searchText+"&local_base=WHU01");//将下载地址转换为Uri类型
           // Uri url = new Uri(jumpUrl + "&local_base=WHU01");
            Search(url);
        }
        public void WeSpider(string searchText)
        {
            //西文搜索
            Console.WriteLine("The Books in West stack are");
            Uri url = new Uri("http://opac.lib.whu.edu.cn/F/" + jumpUrl + "?func=find-b&find_code=WRD&request="+searchText+"&local_base=WHU09");
           // Uri url = new Uri(jumpUrl + "&local_base=WHU09");
            Search(url);

        }
        public void Search(Uri url)
        {
            string BoardStream = "0";//下载内容存入此变量
            int jumplength = 11;
            int BooksCount = 0;
            int matchCount = 0;

            for (; ; )
            {

                HttpWebRequest requestPage = (HttpWebRequest)WebRequest.Create(url);
                WebResponse response = requestPage.GetResponse();
                Stream stream = response.GetResponseStream();//获取页面流
                if (response.ContentType.ToLower().StartsWith("text/"))//如果获得成功（即为文本格式）
                {
                    string pan = "<div class=itemtitle><a href=(.+?)>(.+?)</a>?";
                    string jump = "<script>var tmp=\"(.+?)\";</script>";
                    string f = "超出集合的记录数";//判断是否所有书籍读取完毕
                    string author = "作者：<td class=content>(.*?)<";//作者匹配符
                    StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);//GetEncoding("GB2312"));//读取获得内容流new Regex("<div class=itemtitle><a href=(.+?)>(.+?)</a>?")
                    BoardStream = reader.ReadToEnd();//将内容流转换为文本并存入变量BoardStream，即为所需要的数据流
                    Regex go = new Regex(f);
                    Match goNext = go.Match(BoardStream);
                    if (goNext.Success)//如果匹配完成，级即页面读取完毕，则break
                    {
                        break;
                    }
                    else
                    {
                        Regex r = new Regex(pan);
                        Regex j = new Regex(jump);
                        Regex a = new Regex(author);
                        Match m = r.Match(BoardStream);
                        Match jumpto = j.Match(BoardStream);
                        Match searchAuthor = a.Match(BoardStream);//匹配作者
                        //int matchCount = 0;
                        while (m.Success)
                        {
                            Console.WriteLine("Match" + (++matchCount));

                            for (int l = 1; l <= 2; l++)
                            {
                                Group g = m.Groups[l];
                               
                                Console.WriteLine("Group" + l + "='" + g + "'");
                               
                            }
                            Group authorname = searchAuthor.Groups[1];//找到作者
                            Console.WriteLine("thr Author is" + authorname);
                            m = m.NextMatch();
                            searchAuthor = searchAuthor.NextMatch();
                        }
                        url = new Uri(jumpto.Groups[1] + "?func=short-jump&jump=" + jumplength);//获取分页内容
                        jumplength = jumplength + 10;//分页标签加十
                    }

                }
                BooksCount = BooksCount + 10;
            }
            BooksCount = BooksCount % 2 + matchCount;
            Console.WriteLine("There a " + BooksCount + " books that maybe you want");
        }
    }
}






